package com.fh.service.hydropower;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.util.PageData;

@Service("hydropowerDetailService")
public class HydropowerDetailService {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    /*
     * 新增
     */
    public void save(PageData pd) throws Exception {
        dao.save("HydropowerDetailMapper.save", pd);
    }

    public void insertByBatch(List<PageData> list) throws Exception {
        dao.batchSave("HydropowerDetailMapper.insertByBatch", list);
    }

    /*
     * 删除
     */
    public void delete(PageData pd) throws Exception {
        dao.delete("HydropowerDetailMapper.delete", pd);
    }

    /*
     * 修改
     */
    public void edit(PageData pd) throws Exception {
        dao.update("HydropowerDetailMapper.edit", pd);
    }

    /*
     *列表
     */
    public List<PageData> list(Page page) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.listAlltable", page);
    }
    /*
     *水电表 详情页 列表
     */
    public List<PageData> hydropowerInfoList(Page page) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.datalistPagehydropowerInfoList", page);
    }

    /*
     *列表(全部)
     */
    public List<PageData> listAll(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.listAll", pd);
    }

    /*
     * 通过id获取数据
     */
    public PageData findById(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerDetailMapper.findById", pd);
    }

    /*
     * 通过code获取数据
     */
    public PageData findByCode(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerDetailMapper.findByCode", pd);
    }

    /*
     * 通过code获取最后一次读数数据
     */
    public PageData findLastDegreesByCode(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerDetailMapper.findLastDegreesByCode", pd);
    }

    /*
     * 批量删除
     */
    public void deleteAll(String[] ArrayDATA_IDS) throws Exception {
        dao.delete("HydropowerDetailMapper.deleteAll", ArrayDATA_IDS);
    }

    /*
    <!--查询水电表通过楼宇ID-->
     */
    public List<PageData> listByBuildingId(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.listByBuildingId", pd);
    }

    /*
    <!--通过roomId查询生成账单所需信息-->
     */
    public PageData findContractByRoomId(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerDetailMapper.findContractByRoomId", pd);
    }

    /*
     *抄表生成账单查询合同信息
     */
    public PageData findContractInfo(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerDetailMapper.findContractInfo", pd);
    }

    /*
     *抄表生成账单查询合同信息
     */
    public PageData findContractDatas(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerDetailMapper.findContractDatas", pd);
    }
    /*
     *抄表生成账单查询合同信息 新
     */
    public List<PageData> findContractDatas_new(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.findContractDatas", pd);
    }

    /*
     * 抄表查询电表信息
     */
    public PageData findHydroInfo(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerDetailMapper.findHydroInfo", pd);
    }

    /*
     *抄表查询阶梯价格信息
     */
    public List<PageData> findContractTired(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.findContractTired", pd);
    }

    /*
     *阶梯计价
     */
    public double getTiredPrice(double counts, List<PageData> list) {
        double totalPrice = 0;
        if (list.size() > 0) {
            for (PageData pd : list) {
                double startRead = Double.valueOf(String.valueOf(pd.get("START_READ")));
                double endRead = Double.valueOf(String.valueOf(pd.get("END_READ")));
                double price = Double.valueOf(String.valueOf(pd.get("PRICE")));
                totalPrice += countPrice(startRead, endRead, counts, price);
            }
        }
        return totalPrice;
    }

    /*
     * 阶梯计价每段价格
     */
    public double countPrice(double s, double e, double c, double p) {
        double price = 0;
        if (c > e) {
            price = p * (e - s);
        } else if (c > s && c < e) {
            price = p * (c - s);
        } else {
            price = 0;
        }
        return price;
    }
    /*
     * 电表详情页 详情数据
     */
    public List<PageData> find_info_by_EID(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.find_info_by_EID", pd);
    }
    /*
     * 总量 分表  详情数据
     */
    public List<PageData> amount_difference(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.amount_difference", pd);
    }
    /*
     * 分表  详情数据
     */
    public List<PageData> childern_difference(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.childern_difference", pd);
    }
    /*
     * 抄表查询电表信息
     */
    public  List<PageData> findHydroInfo_new(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.findHydroInfo_new", pd);
    }
    /*
     * 查询上一条信息
     */
    public  List<PageData> findHydroInfo_last(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.findHydroInfo_last", pd);
    }
    /*
     * 删除抄表记录
     */
    public void del_GenerateBills(PageData pd) throws Exception {
        dao.delete("HydropowerDetailMapper.del_GenerateBills", pd);
    }
    /*
     * 查询本次抄表数大于上一次
     */
    public  List<PageData> findHyDetail_last(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerDetailMapper.findHyDetail_last", pd);
    }
}

